//
//  DeleteCookieCacheViewController.m
//  WKWebViewDemo
//
//  Created by migu-hfy on 16/3/29.
//  Copyright © 2016年 migu-hfy. All rights reserved.
//

#import "DeleteCookieCacheViewController.h"
#import <WebKit/WebKit.h>

@interface DeleteCookieCacheViewController ()

@end

@implementation DeleteCookieCacheViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self deleteCookieAndCacheInIOS9];
    
    [self deleteCookieAndCacheInIOS9Pre];
}

- (void)deleteCookieAndCacheInIOS9
{
    NSSet *websiteDataTypes
    = [NSSet setWithArray:@[
                            WKWebsiteDataTypeDiskCache,
                            WKWebsiteDataTypeOfflineWebApplicationCache,
                            WKWebsiteDataTypeMemoryCache,
                            WKWebsiteDataTypeLocalStorage,
                            WKWebsiteDataTypeCookies,
                            WKWebsiteDataTypeSessionStorage,
                            WKWebsiteDataTypeIndexedDBDatabases,
                            WKWebsiteDataTypeWebSQLDatabases
                            ]];
    //也可以用下面方法
    //NSSet *websiteDataTypes = [WKWebsiteDataStore allWebsiteDataTypes];
    
    NSDate *dateFrom = [NSDate dateWithTimeIntervalSince1970:0];
  
    [[WKWebsiteDataStore defaultDataStore] removeDataOfTypes:websiteDataTypes modifiedSince:dateFrom completionHandler:^{
        //删除完成
    }];
}

- (void)deleteCookieAndCacheInIOS9Pre
{
    //删除Cookie
    NSString *libraryPath = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, NO).firstObject;
    NSString *cookiePath = [libraryPath stringByAppendingPathComponent:@"Cookies"];
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    NSError *error;
    
    if (![fileManager removeItemAtPath:cookiePath error:&error]) {
        NSLog(@"%@",error.localizedDescription);
    }
    
    //删除Cache
    [[NSURLCache sharedURLCache] removeAllCachedResponses];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
